package com.moss.cloud.base.biz.controller;

import com.moss.cloud.common.core.model.Result;
import com.moss.cloud.common.core.utils.SpringContextHolder;
import com.moss.cloud.common.core.utils.UserContextHolder;
import com.moss.cloud.common.file.model.UploadFrom;
import com.moss.cloud.common.file.model.UploadRes;
import com.moss.cloud.common.file.service.IStorageService;
import com.moss.cloud.common.security.annotation.SysLog;
import com.moss.cloud.common.security.event.DemoEvent;
import com.moss.cloud.common.sms.model.SmsDTO;
import com.moss.cloud.common.sms.service.ISmsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 测试接口
 */
@Slf4j
@RestController
@RequestMapping("/user")
public class TestController {
    @Resource
    IStorageService storageService;
    @Resource
    ISmsService smsService;
    // 在白名单的接口
    @PostMapping("/findUserId")
    public Result getUser(){
        SpringContextHolder.publishEvent(new DemoEvent("source","方法"));
        return Result.success("findUserId");
    }
    // 不在白名单的接口，再资源池中并且admin拥有权限
    @SysLog(serverName = "moss-cloud-base",operation = "用户信息查询")
    @GetMapping("/conditions")
    public Result getUserBuyId(){
        log.info("当前登录用户信息：{}", UserContextHolder.getUserId());
        return Result.success("getUserBuyId");
    }
    // 不在资源池中
    @PostMapping("/addUser")
    public Result addUser(){
        return Result.success("addUser");
    }
    // 在资源池中没有被赋予权限
    @PostMapping("/editUser")
    public Result editUser(){
        return Result.success("editUser");
    }

    @PostMapping("/upload")
    public Result upload(@RequestBody UploadFrom uploadFrom){
        UploadRes uploadRes = storageService.uploadBase64(uploadFrom);
        return Result.success(uploadRes);
    }

    @PostMapping("/removeFile")
    public Result removeFile(String fileId){
        storageService.removeFile(fileId);
        return Result.success("删除成功");
    }

    @PostMapping("/sendSms")
    public Result sendSms(){
        SmsDTO smsDTO = SmsDTO.builder().phoneNumbers(new String[]{"+8618809549852"})
                .sign("七心海棠")
                .templateID("358686")
                .templateParams(new String[]{"234567","5"}).build();
        smsService.sendSms(smsDTO);
        return Result.success();
    }
}
